//
//  HYBookSeatPlaneVC.m
//  Booking
//
//  Created by Maybe_文仔 on 2018/7/29.
//  Copyright © 2018年 Duntech. All rights reserved.
//

#import "HYBookSeatPlaneVC.h"
#import "SeatBezierView.h"
#import "HYBookingRoomFilterView.h"
#import "HYDateCitySelectView.h"
#import "HYSeatInfoView.h"
#import "HYMeetingScheduleVC.h"

@interface HYBookSeatPlaneVC ()<UIScrollViewDelegate>

@property (nonatomic, strong) HYBookingRoomFilterView *filterView;
@property (nonatomic, strong) HYDateCitySelectView *dateCitySelectView;
@property (nonatomic, assign) BOOL isShowSelectView;
@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, strong) UIImageView *imageView;
@property (nonatomic, strong) SeatBezierView *bezierView;
@property (nonatomic, strong) HYSeatInfoView *seatInfoView;

@property (nonatomic, strong) UIView *topView;
@property (nonatomic, strong) UILabel *avalibleLab;
@property (nonatomic, strong) UILabel *avalibleDesLab;
@property (nonatomic, strong) UILabel *unAvalibleLab;
@property (nonatomic, strong) UILabel *unAvalibleDesLab;
@end

@implementation HYBookSeatPlaneVC

- (void)viewDidLoad {
    
    [super viewDidLoad];
    [self setupSubViews];
}

- (void)setupNav{
    
    [super setupNav];
    self.title = @"Book Seat";
}

-(void)setupSubViews{
    
    [self.view addSubview:self.filterView];
    [self.view addSubview:self.topView];
    [self.topView addSubview:self.avalibleLab];
    [self.topView addSubview:self.avalibleDesLab];
    [self.topView addSubview:self.unAvalibleLab];
    [self.topView addSubview:self.unAvalibleDesLab];
    
    [_filterView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.equalTo(self.view);
//        make.height.mas_equalTo(KTopFilterViewHeight);
        make.height.mas_equalTo(0);
    }];
    
    [self.topView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.filterView.mas_bottom);
        make.left.right.mas_equalTo(self.view);
        make.height.mas_equalTo(36);
    }];
    
    [self.avalibleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.topView.mas_centerY);
        make.left.mas_equalTo(self.topView.mas_left).mas_offset(10);
        make.size.mas_equalTo(CGSizeMake(13, 13));
    }];
    
    [self.avalibleDesLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.topView.mas_centerY);
        make.left.mas_equalTo(self.avalibleLab.mas_right).mas_offset(10);
    }];
    
    [self.unAvalibleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.topView.mas_centerY);
        make.left.mas_equalTo(self.avalibleDesLab.mas_right).mas_offset(10);
        make.size.mas_equalTo(CGSizeMake(13, 13));
    }];
    
    [self.unAvalibleDesLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.topView.mas_centerY);
        make.left.mas_equalTo(self.unAvalibleLab.mas_right).mas_offset(10);
    }];
    
    NSDictionary *areaListDic = [self readLocalFileWithName:@"SeatList"];
    NSLog(@"%@",areaListDic);
    //缩放图
    UIScrollView *scrollView = [[UIScrollView alloc] init];
    scrollView.backgroundColor = KAPP_WHITE_COLOR;
    scrollView.showsVerticalScrollIndicator = NO;
    scrollView.showsHorizontalScrollIndicator = NO;
    self.scrollView = scrollView;
    [self.view addSubview:scrollView];
    
    //底图
    UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"seat_app_bg"]];
    imageView.backgroundColor = [UIColor redColor];
    self.imageView = imageView;
    [scrollView addSubview:imageView];
    
    //画布
    SeatBezierView *beView = [[SeatBezierView alloc] initWithFrame:CGRectMake(0, 0, imageView.image.size.width, imageView.image.size.height)];
    beView.areaList = areaListDic[@"areas"];
    beView.backgroundColor = [UIColor clearColor];
    self.bezierView = beView;
    [imageView addSubview:beView];
    
    //重新规划大小
    CGFloat realH = imageView.image.size.height;
    CGFloat realW = imageView.image.size.width;
    CGFloat scaleWH = realH/realW;
    CGFloat scale = self.view.frame.size.width/realW;
    
    [scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.topView.mas_bottom);
        make.left.right.bottom.mas_equalTo(self.view);
//        make.size.mas_equalTo(CGSizeMake(self.view.frame.size.width, self.view.frame.size.width*scaleWH));
    }];
//    scrollView.contentSize = imageView.image.size;
    
    // 设置代理
    self.scrollView.delegate = self;
    
    // 设置缩放比例
    self.scrollView.maximumZoomScale = 2.0;
    self.scrollView.minimumZoomScale = scale;
    self.scrollView.zoomScale = scale;
    
    //点击可选会议室展示会议室信息的view
    [self.view addSubview:self.seatInfoView];
    [self.seatInfoView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.bottom.right.equalTo(self.view);
        make.height.mas_equalTo(80*WIDTH_MULTIPLE);
    }];
}

#pragma mark - action
- (void)tapFilterViewAction{
    
    if (!self.isShowSelectView) {
        
        [self.dateCitySelectView showDateCitySelectViewInView:self.view];
    }
    else{
        [self.dateCitySelectView hideDateCitySelectView];
    }
    self.isShowSelectView = !self.isShowSelectView;
}

-(void)loadMeetingSeatDetailInfoWithSeatId:(NSString *)seatId{
    kWeakSelf(weakSelf);
    [HYNetRequestHandle getSeatDetailWithSeatID:seatId complection:^(HYBaseNetworkModel *baseModel) {
        HYSeatCellModel *model = [HYSeatCellModel modelWithDictionary:baseModel.result];
        weakSelf.seatInfoView.seatModel = model;
    }];
}

#pragma mark -- 这个方法的返回值决定了要缩放的内容（返回值只能是UIScrollView的子控件）

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
    return self.imageView;
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    UITouch *touch=[touches anyObject];
    if (touch.view == self.scrollView) {
        //页面上的点坐标
        CGPoint point = [touch locationInView:[touch view]];
        NSLog(@"touch_x:%f,touch_y:%f",point.x/self.scrollView.zoomScale,point.y/self.scrollView.zoomScale);
        //按比例换算后的实际点坐标
        CGPoint realPoint = CGPointMake(point.x/self.scrollView.zoomScale, point.y/self.scrollView.zoomScale);
        kWeakSelf(weakSelf);
        [self.bezierView containsPoint:realPoint complete:^(NSString *roomId) {
            if (weakSelf.seatInfoView.isHidden) {
                weakSelf.seatInfoView.hidden = NO;
            }
            [weakSelf loadMeetingSeatDetailInfoWithSeatId:roomId];
        }];
    }
}

//读取本地json测试文件
- (NSDictionary *)readLocalFileWithName:(NSString *)name {
    // 获取文件路径
    NSString *path = [[NSBundle mainBundle] pathForResource:name ofType:@"json"];
    // 将文件数据化
    NSData *data = [[NSData alloc] initWithContentsOfFile:path];
    // 对数据进行JSON格式化并返回字典形式
    return [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
}

#pragma mark - lazyload

-(UIView *)topView{
    if (!_topView) {
        _topView = [UIView new];
        _topView.backgroundColor = KAPP_WHITE_COLOR;
    }
    return _topView;
}

-(UILabel *)avalibleLab{
    if (!_avalibleLab) {
        _avalibleLab = [UILabel new];
        _avalibleLab.backgroundColor = KAPP_Available_COLOR;
    }
    return _avalibleLab;
}

-(UILabel *)avalibleDesLab{
    if (!_avalibleDesLab) {
        _avalibleDesLab = [UILabel new];
        _avalibleDesLab.text = NSLocalizedString(@"available", nil);
        _avalibleDesLab.font = KFont(15);
    }
    return _avalibleDesLab;
}

-(UILabel *)unAvalibleLab{
    if (!_unAvalibleLab) {
        _unAvalibleLab = [UILabel new];
        _unAvalibleLab.backgroundColor = KAPP_7b7b7b_COLOR;
    }
    return _unAvalibleLab;
}

-(UILabel *)unAvalibleDesLab{
    if (!_unAvalibleDesLab) {
        _unAvalibleDesLab = [UILabel new];
        _unAvalibleDesLab.text = NSLocalizedString(@"unavailable", nil);
        _unAvalibleDesLab.font = KFont(15);
    }
    return _unAvalibleDesLab;
}

-(NSMutableArray *)dataSetsArray{
    if (!_dataSetsArray) {
        _dataSetsArray = [NSMutableArray new];
    }
    return _dataSetsArray;
}

- (HYBookingRoomFilterView *)filterView{
    
    if (!_filterView) {
        _filterView = [HYBookingRoomFilterView new];
        [_filterView setHideSwitch];
        _filterView.userInteractionEnabled = YES;
        UITapGestureRecognizer *tapGes = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapFilterViewAction)];
        [_filterView addGestureRecognizer:tapGes];
    }
    return _filterView;
}

- (HYDateCitySelectView *)dateCitySelectView{
    
    if (!_dateCitySelectView) {
        
        _dateCitySelectView = [HYDateCitySelectView new];
        _dateCitySelectView.viewController = self;
        kWeakSelf(weakSelf);
        _dateCitySelectView.dateCitySearchBlock = ^(NSDate *date, NSString *startTime, NSString *endTime, NSString *cityID, NSString *buildingID, NSString *city, NSString *building) {
            
            DLog(@"date:%@,startTime:%@,endTime:%@,cityID:%@,buildingID:%@,city:%@,building:%@",date,startTime,endTime,cityID,buildingID,city,building);
            
            //            weakSelf.selectBuildingID = buildingID;
            //            weakSelf.selectCityID = cityID;
            //
            //
            //            [HYNetRequestHandle getAvailableMeetingRoomWithPageNo:nil pageSize:nil status:nil city:cityID build:buildingID floor:nil markName:nil Complection:^(NSArray *availableMeetingRoomList) {
            //                [weakSelf.sectionArray removeAllObjects];
            //                [weakSelf.dataSetsArray removeAllObjects];
            //                [availableMeetingRoomList enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            //
            //                    HYMeetingRoomModel *model = [HYMeetingRoomModel modelWithDictionary:obj];
            //                    model.cellIdientifier = @"HYMeetingListCell";
            //                    model.cellHeight = 140 * WIDTH_MULTIPLE;
            //                    [weakSelf.sectionArray addObject:model];
            //                }];
            
            //            }];
        };
    }
    return _dateCitySelectView;
}

-(HYSeatInfoView *)seatInfoView{
    if (!_seatInfoView) {
        _seatInfoView = [HYSeatInfoView new];
        _seatInfoView.hidden = YES;
        kWeakSelf(weakSelf);
        _seatInfoView.bookSeatBlock = ^(HYSeatCellModel *seatModel) {
            
            HYMeetingScheduleVC *scheduleVC = [HYMeetingScheduleVC new];
            scheduleVC.seatModel = seatModel;
            scheduleVC.scheduleType = SeatSchedule;
            scheduleVC.meetingDate = weakSelf.meetingDate;
            scheduleVC.startTime = weakSelf.startTime;
            scheduleVC.endTime = weakSelf.endTime;
            [weakSelf.navigationController pushViewController:scheduleVC animated:YES];
        };
    }
    return _seatInfoView;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
